<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form class="login_form" :model="loginForm" :rules="rules" ref="loginFormRef">
          <h1>Hello</h1>
          <h2>欢迎来到文章管理平台</h2>
          <el-form-item prop="username">
            <el-input :prefix-icon="User" v-model="loginForm.username"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input type="password" :prefix-icon="Lock" v-model="loginForm.password" show-password></el-input>
          </el-form-item>
          <el-form-item>
            <el-button :loading=loading class="login_btn" type="primary" size="default" @click="login"
              :disabled="loading">登录</el-button>
          </el-form-item>
        </el-form>
      </el-col>

    </el-row>
  </div>
</template>

<script setup lang="ts">
import { User, Lock } from '@element-plus/icons-vue';
import { reactive, ref } from 'vue';
import { ElNotification, ElMessage } from 'element-plus'
import { getTime } from '@/utils/time';
import { useRouter, useRoute } from 'vue-router';
import useUserStore from '@/store/modules/user';
let loading = ref(false)

let $router = useRouter()
let $route = useRoute()

let userStore = useUserStore()
let loginForm = reactive({
  username: "admin",
  password: "123456"
})

const validatorUserName = (rules: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('请输入用户名'))
  } else if (value.length < 5 || value.length > 10) {
    callback(new Error('账号长度在 5 到 10 个字符之间'))
  } else {
    callback()
  }
}
const validatorPasssWord = (rules: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('请输入密码'))
  } else if (value.length < 6 || value.length > 15) {
    callback(new Error('长度在 6 到 15 个字符'))
  } else {
    callback()
  }
}

const rules = {
  username: [
    // { required: true, message: '请输入用户名', trigger: 'blur' },
    // { min: 5, max: 10, message: '账号长度在 5 到 10 个字符之间', trigger: 'change' }
    {
      trigger: 'change', validator: validatorUserName
    }
  ],
  password: [
    // { required: true, message: '请输入密码', trigger: 'blur' },
    // { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'change' }
    { trigger: 'change', validator: validatorPasssWord }
  ]
}

let loginFormRef = ref()


// 登录按钮回调
const login = async () => {
  // 保证全部校验成功再发请求
  try {
    await loginFormRef.value.validate();
  } catch (error) {
    ElNotification({
      message: "请输入正确的账号密码",
      type: 'error'
    })
    return;
  }

  loading.value = true;


  try {
    let res = await userStore.userLogin(loginForm);
    if (res === "ok") {
      // 三目运算符 作用： 如果存在redirect就跳转redirect，否则跳转首页
      $router.push($route.query.redirect ? $route.query.redirect as string : '/');

      ElNotification({
        title: 'Hi!' + getTime(),
        message: '欢迎回来',
        type: 'success'
      })
    } else {
      ElMessage({
        message: "账号密码错误",
        type: 'error'
      })
    }
  } catch (error: any) {
    ElNotification({
      message: error.message,
      type: 'error'
    })
  } finally {
    loading.value = false;
  }

}

</script>

<style lang="scss" scoped>
.login_container {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: cover;
}

.login_form {
  position: relative;
  width: 80%;
  top: 30vh;
  background: url("@/assets/images/login_form.png") no-repeat;
  background-size: cover;
  padding: 40px;

  h1 {
    color: white;
    font-size: 40px;
  }

  h2 {
    color: white;
    font-size: 20px;
    margin: 20px 0px;
  }

  .login_btn {
    width: 100%;
    border: none;
  }
}
</style>
